/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.gehj.alluitls;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * asset与raw文件互转
 *
 */
public class ResourceFileUtils {
    /**
     * 获取assets目录下的网页
     * 这种方式可以加载assets目录下的网页，并且与网页有关的css，js，图片等文件也会的加载。
     * webView.loadUrl("file:///android_asset/html/index.html");
     *
     * @param filePath
     */
    public static String getHtml(String filePath) {
        return "file:///android_asset/" + filePath;
    }



    /**
     * 获取所有文件
     *
     * @param path A relative path within the assets, i.e., "docs/home.html"
     * @return 返回文件名 用context.getAssets().open(fileName)打开；
     */
    public static String[] getfiles(Context context, String path) {
        AssetManager assetManager = context.getAssets();
        String[] files = null;
        try {
            files = assetManager.list(path);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            assetManager.close();
        }
        return files;
    }


    /**
     * 获取assets目录下的图片资源
     *
     * @param fileName
     */
    public static Bitmap getPic(Context context, String fileName) {
        InputStream is = null;
        Bitmap bitmap = null;
        try {
            is = context.getAssets().open(fileName);
            bitmap = BitmapFactory.decodeStream(is);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(is);
        }
        return bitmap;
    }



    /**
     * 获取assets目录下的文本资源 -->String
     *
     * @param fileName
     */
    public static String getTex(Context context, String fileName) {
        InputStream is = null;
        String result = "";
        try {
            is = context.getAssets().open(fileName);
            int lenght = is.available();
            byte[] buffer = new byte[lenght];
            is.read(buffer);
            result = new String(buffer, "utf8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(is);
        }
        return result;

    }


    /**
     * 获取assets目录下的音频资源
     *
     * @param fileName 用注释的方法去播放
     */
    public static AssetFileDescriptor getAudio(Context context, String fileName) {
        AssetFileDescriptor afd = null;
        try {
            // 打开指定音乐文件,获取assets目录下指定文件的AssetFileDescriptor对象
            afd = context.getAssets().openFd(fileName);
//            MediaPlayer mPlayer=new MediaPlayer();
//            mPlayer.reset();
//        // 使用MediaPlayer加载指定的声音文件。
//            mPlayer.setDataSource(afd.getFileDescriptor(),
//                    afd.getStartOffset(), afd.getLength());
//            // 准备声音
//            mPlayer.prepare();
//        // 播放
//            mPlayer.start();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(afd);
        }
        return afd;

    }


    /**
     * 获取assets目录下的URI
     * 可用于播放视频资源
     *
     * @param fileName
     * @return 用注释的内容去播放
     */
    public static Uri getUri(Context context, String fileName) {
        File file = getFile(context, fileName);
        //播放视频
//        VideoView mVideoView = new VideoView(context);
//        mVideoView.setVideoURI(Uri.fromFile(file));
//        mVideoView.start();
        return Uri.fromFile(file);
    }


    /**
     * 从assets目录中复制内容到sd卡中
     *
     * @param context   Context 使用CopyFiles类的Activity
     * @param assetPath String  原文件路径  如：/aa
     * @param newPath   String  复制后路径  如：xx:/bb/cc
     */
    public static void copyFilesFassets(Context context, String assetPath, String newPath) {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            String fileNames[] = context.getAssets().list(assetPath);//获取assets目录下的所有文件及目录名
            if (fileNames.length > 0) {//如果是目录
                File file = new File(newPath);
                file.mkdirs();//如果文件夹不存在，则递归
                for (String fileName : fileNames) {
                    copyFilesFassets(context, assetPath + "/" + fileName, newPath + "/" + fileName);
                }
            } else {//如果是文件
                is = context.getAssets().open(assetPath);
                fos = new FileOutputStream(new File(newPath));
                byte[] buffer = new byte[1024];
                int byteCount = 0;
                while ((byteCount = is.read(buffer)) != -1) {//循环从输入流读取 buffer字节
                    fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
                }
                fos.flush();//刷新缓冲区
                is.close();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //如果捕捉到错误则通知UI线程
        } finally {
            close(is, fos);
        }
    }


    /**
     * 将流拷贝到本地 然后返回本地file  默认拷贝到Files目录
     *
     * @param context
     * @param name
     * @return
     */
    public static File getFile(Context context, String name) {
        InputStream is = null;
        FileOutputStream os = null;
        try {
            File dir = context.getFilesDir();
            File file = new File(dir, name);
            if (file.exists()) {
                return file;

            } else {
                file.createNewFile();
                os = new FileOutputStream(file);
                is = context.getAssets().open(name);
                byte[] buffer = new byte[1024];
                int bufferRead = 0;
                while ((bufferRead = is.read(buffer)) != -1) {
                    os.write(buffer, 0, bufferRead);
                }
                os.flush();
                is.close();
                os.close();
                Log.d("Test", "=========getFile success=========");
                return file;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(is, os);
        }
        return null;
    }




    /**
     * 获取raw目录下的流
     *
     * @param resId 资源id
     */
    public static InputStream getRawStream(Context context, int resId) {
        return context.getResources().openRawResource(resId);

    }



    public static void playVoice(Context context,int ResId){
        final MediaPlayer[] mp = {MediaPlayer.create(context, ResId)};
            mp[0].setLooping(false);
            mp[0].setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mp[0].release();
                    mp[0] = null;
                }
            });
            mp[0].start();
        }




    /**
     * 获取raw目录下的输入流
     *
     * @param resId 资源id
     */
    public static Uri getRawUri(Context context, int resId) {
        String s_uri ="android.resource://" + context.getPackageName() + "/" + resId;
        Uri uri = Uri.parse(s_uri);
        return uri ;
    }

    /**
     * 关闭流
     *
     * @param is
     */
    private static void close(Closeable... is) {
        for (Closeable i : is) {
            try {
                i.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    public static File rawToFile(Context context,int Res,String fileName) throws IOException {
        InputStream rawStream = getRawStream(context, Res);
        File parent;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//是否有sd卡
            parent = context.getExternalCacheDir();
        }else {
            parent = context.getCacheDir();
        }

        File file = new File(parent,fileName);
        OutputStream outputStream = new FileOutputStream(file);
        byte[] buffer = new byte[rawStream.available()];
        rawStream.read(buffer);
        outputStream.write(buffer);
        outputStream.flush();
        rawStream.close();
        outputStream.close();
        return  file;
    }


    public static File BitmapToFile(Bitmap bitmap,String imgPath){

        File file= null;
        try {
            file = new File(imgPath);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return file;
    }

}
